package com.yanqu.road.server.cmd.activity.seacraftsouthsea;

import com.yanqu.road.pb.activity.SeacraftSouthSeaProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftSouthSeaMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Description
 * @Author cwq
 * @Data 2022/1/20 15:28
 */
@Cmd(code = GameProtocol.S_SEACRAFT_SOUTH_SEA_GUESS, desc = "海上争霸 南海丝路 竞猜")
public class SeacraftSouthSeaGuessCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SeacraftSouthSeaProto.SeacraftSouthSeaGuessReqMsg reqMsg = SeacraftSouthSeaProto.SeacraftSouthSeaGuessReqMsg.parseFrom(packet.getBytes());
        ret = SeacraftSouthSeaMgr.canEntryActivity(player,reqMsg.getActivityId());
        if(ret != 0){
            return;
        }

        Set<String> set = new HashSet<>(reqMsg.getUnionListList());
        if(reqMsg.getUnionListList().size() == 0 || set.size() != reqMsg.getUnionListList().size()){
            ret = GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_GUESS_UNION_NUM_ERROR;
            return;
        }

        // 判断联盟数量
        int num = SeacraftSouthSeaMgr.getConfig().getGuessUnionNumMap().getOrDefault(reqMsg.getRound(),0);
        if(num == 0){
            ret = GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_NO_HAVE_GUESS_REWARD;
            return;
        }
        if(reqMsg.getUnionListList().size() > num ){
            ret = GameErrorCode.E_SEACRAFT_SOUTH_SEA_ACTIVITY_GUESS_UNION_NUM_ERROR;
            return;
        }
        SeacraftSouthSeaProto.SeacraftSouthSeaGuessGameToCrossMsg.Builder gameToCrossMsg = SeacraftSouthSeaProto.SeacraftSouthSeaGuessGameToCrossMsg.newBuilder();
        gameToCrossMsg.setActivityId(reqMsg.getActivityId());
        gameToCrossMsg.setStcMsg(reqMsg);
        gameToCrossMsg.setUnionId(player.getUserInfo().getUnionUid());

        player.sendUnionActivityGroupPacket(CrossProtocol.C2_SEACRAFT_SOUTH_SEA_GUESS, gameToCrossMsg, reqMsg.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            SeacraftSouthSeaProto.SeacraftSouthSeaGuessRespMsg.Builder resp = SeacraftSouthSeaProto.SeacraftSouthSeaGuessRespMsg.newBuilder();
            resp.setRet(ret);
            player.sendPacket(ClientProtocol.U_SEACRAFT_SOUTH_SEA_GUESS,resp);
        }
    }
}
